package com.example.network_services;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RetrieveDataTask extends AsyncTask<String, Void, String> {

	private EditText givenUrl;
	private EditText urlContents;
	private TextView labelText;
	
	private HttpClient client;
	private HttpGet httpGet;
	private ResponseHandler<String> handler;
	
	public RetrieveDataTask(EditText givenUrl, EditText urlContents, TextView labelText)
	{
		this.givenUrl = givenUrl;
        this.urlContents = urlContents;
        this.labelText = labelText;
	}
	
	@Override
	protected String doInBackground(String... params) {
		String result = null;
		
		if (givenUrl.length() != 0){
			String requestUri = params[0];
			
			client = new DefaultHttpClient();
			httpGet = new HttpGet(requestUri);
			handler = new BasicResponseHandler();
			
			try {
				result = client.execute(httpGet, handler);	
			} catch(IllegalStateException e){
				//Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
			} catch (Exception e) {
				//Toast.makeText(getApplicationContext(), e.getClass().toString(), Toast.LENGTH_LONG).show();
				e.getClass();
				e.printStackTrace();
			} finally {
				
			}
		} else {
			//givenUrl.setText("#Voer hier een url in!");
		}
		
		return result;
	}

	@Override
	protected void onPostExecute(String htmlData) {
		super.onPostExecute(htmlData);
		
		urlContents.setVisibility(View.VISIBLE);
		urlContents.setText(htmlData);
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
	}

	@Override
	protected void onProgressUpdate(Void... values) {
		super.onProgressUpdate(values);
	}
}
